package com.shkco.xbjcc.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shkco.xbjcc.dto.CourseDto;
import com.shkco.xbjcc.service.ICourseService;
import com.shkco.xbjcc.domain.Course;
import com.shkco.xbjcc.query.CourseQuery;
import com.shkco.result.JSONResult;
import com.shkco.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody @Valid CourseDto courseDto){
        courseService.save(courseDto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }

    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> wrapper=new EntityWrapper<>();
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }


    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    public JSONResult onLineCourse(@PathVariable("id") Long id){
        courseService.onLineCourse(id);
        return JSONResult.success();
    }

    @RequestMapping(value="/offLineCourse/{id}",method= RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("id") Long id){
        courseService.offLineCourse(id);
        return JSONResult.success();
    }

    @RequestMapping(value = "/detail/data/{courseId}",method = RequestMethod.GET)
    public JSONResult getDetail(@PathVariable("courseId")Long courseId){
        return JSONResult.success(courseService.getDetail(courseId));
    }

    @RequestMapping(value = "/info/{courseIds}",method = RequestMethod.GET)
    public JSONResult info(@PathVariable("courseIds")String courseIds){
        return JSONResult.success(courseService.info(courseIds));
    }

}
